package coms.action;

import com.opensymphony.xwork2.Action;
import coms.delegates.ConferenceDelegate;
import coms.delegates.LocationDelegate;
import coms.dto.Conference;
import coms.dto.Location;
import coms.dto.User;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ConferenceAction extends ComsActionSupport implements IAction {

    private int conferenceId;
    private Conference conference;
    private List<Conference> conferenceList;
    private ConferenceDelegate conferenceDelegate;
    private List<Location> locationList;
    private LocationDelegate locationDelegate;
    private String action;
    private boolean admin;

    public boolean isAdmin() {
          User u = (User) getSession().get("user");
        if (u != null) {
            admin = u.isAdmin();
        } 
        return admin;
    }

    public void setAdmin(boolean admin) {
        this.admin = admin;
    }

    public ConferenceAction() {

        conference = new Conference();

        conferenceList = new ArrayList<Conference>();
        conferenceDelegate = new ConferenceDelegate();

        locationList = new ArrayList<Location>();
        locationDelegate = new LocationDelegate();

    }

    private void initForm() {
        User u=(User)getSession().get("user");
        if(u!=null)
        {
            admin=u.isAdmin();
        }        
        try {
            locationList = locationDelegate.findAll();
        } catch (Exception ex) {
            Logger.getLogger(ConferenceAction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void populateDefault() {
        conference.setCost(0);
        Date today = Calendar.getInstance().getTime();
        conference.setConferenceStartDate(today);
        conference.setConferenceEndDate(today);
        conference.setRegistrationEndDate(today);
        conference.setRegistrationStartDate(today);
        conference.setReviewEndDate(today);
        conference.setReviewStartDate(today);
        conference.setSubmissionEndDate(today);
        conference.setSubmissionStartDate(today);
    }

    private void populate() {
        try {
            conference = conferenceDelegate.getById(conferenceId);
        } catch (Exception ex) {
            Logger.getLogger(ConferenceAction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public String input() {
        
        initForm();
        populateDefault();
        action = "createConference_";
        return "input";
    }

    @Override
    public String edit() {
        initForm();
        populate();
        action = "updateConference_" + conference.getId();
        return "edit";
    }

    @Override
    public String create() {
        User u = (User) getSession().get("user");
        if (u != null) {
            admin = u.isAdmin();
        } else {
            return "error";
        }
        conferenceDelegate.create(conference);
        return Action.SUCCESS;
    }

    @Override
    public String update() {
        try {
            User u = (User) getSession().get("user");
            if (u != null) {
                admin = u.isAdmin();
            } else {
                return "error";
            }
            System.out.println("############## update id:" + conference.getId());
            conferenceDelegate.update(conference);
        } catch (Exception ex) {
            return "error";
        }

        return "updated";
    }

    @Override
    public String delete() {
        User u=(User)getSession().get("user");
        if(u!=null)
        {
            admin=u.isAdmin();
        }
        else 
        {
            return "error";
        }
        populate();
        conferenceDelegate.delete(conference);
        return "deleted";
    }

    @Override
    public String findAll() {
        User u=(User)getSession().get("user");
        if(u!=null)
        {
            admin=u.isAdmin();
        }
        else 
        {
            return "error";
        }
        try {
            conferenceList = conferenceDelegate.findAll();
        } catch (Exception ex) {
            return "error";
        }
        return Action.SUCCESS;
    }

    @Override
    public String search() {
        
        initForm();
        try {
            conferenceList = conferenceDelegate.search(conference);
        } catch (Exception ex) {
            return "error";
        }
        return "search";
    }

    public Conference getConference() {
        return conference;
    }

    public void setConference(Conference conference) {
        this.conference = conference;
    }

    public List<Conference> getConferenceList() {
        return conferenceList;
    }

    public void setConferenceList(List<Conference> conferenceList) {
        this.conferenceList = conferenceList;
    }

    public List<Location> getLocationList() {
        return locationList;
    }

    public void setLocationList(List<Location> locationList) {
        this.locationList = locationList;
    }

    public void setLocation(int locationId) {
        try {
            Location location = locationDelegate.getById(locationId);
            conference.setLocation(location);
        } catch (Exception ex) {
            Logger.getLogger(ConferenceAction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public String getConferenceId() {
        return "" + conferenceId;
    }

    public void setConferenceId(String conferenceId) {
        try {
            if (conferenceId != null && !conferenceId.equals("")) {
                this.conferenceId = Integer.parseInt(conferenceId);
                conference.setId(this.conferenceId);
            }
        } catch (Exception ex) {
            Logger.getLogger(ConferenceAction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }
}
